﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AllDemo
{
    public partial class 直角坐标系转换 : Form
    {
        public 直角坐标系转换()
        {
            InitializeComponent();
        }


        private PointF o1=new PointF(300,500);        //第一个坐标系的原点
        private PointF o2=new PointF(300, 500);        //第二个坐标系的原点
        private PointF point1 = new PointF(500, 350);       //需要转换的点
        private float rotationAngle = (float)(30.5*Math.PI/180);       //第二个坐标系原点相对第一个坐标系偏移角度

        private float x_lenth = 300f;                 //x轴长度
        private float y_lenth = 300f;                 //y轴长度
      
        private void btn_plot_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();

            g.DrawLine(Pens.Orange, o1.X , o1.Y, o1.X + x_lenth, o1.Y);  //绘制X轴
            g.DrawLine(Pens.Orange, o1.X, o1.Y, o1.X , o1.Y-y_lenth);    //绘制Y轴
            g.DrawLine(Pens.OrangeRed, o2.X, o2.Y, point1.X, point1.Y);  //绘制r

            //X轴Y轴需要的长度是已知的
            PointF line2_1EndPoint = new PointF();
            PointF line2_2EndPoint = new PointF();
            line2_1EndPoint = Transition(o1.X-o1.X,y_lenth, rotationAngle,2);
            line2_2EndPoint = Transition(x_lenth, o1.Y - o1.Y,rotationAngle,2);
            g.DrawLine(Pens.Black, o1.X, o1.Y, line2_1EndPoint.X+o1.X, line2_1EndPoint.Y+o1.Y);
            g.DrawLine(Pens.Black, o1.X, o1.Y, line2_2EndPoint.X + o1.X, line2_2EndPoint.Y + o1.Y);

            g.DrawLine(Pens.DarkOliveGreen, point1.X - 5, point1.Y, point1.X + 5, point1.Y);     //绘制需要换算坐标系的点
            g.DrawLine(Pens.DarkOliveGreen, point1.X, point1.Y - 5, point1.X, point1.Y + 5);     //绘制需要换算坐标系的点

            //选择字体、字号、风格
            Font font = new Font("Adobe Gothic Std", 10f, FontStyle.Bold);
            g.DrawString("该点在原坐标系位置：[" + (point1.X-o1.X)+":"+(-(point1.Y-o1.Y))+"]", font, Brushes.Orange, point1.X + 5, point1.Y - 15);
            //此处为什么是逆时针旋转？因为新坐标系相对于原坐标系是顺时针旋转，那么是不是将该点逆时针旋转同样的角度，然后计算它在原坐标系的位置就等同于它在新坐标系的位置
            PointF newPoint = Transition(point1.X-o1.X,o1.Y-point1.Y, rotationAngle,1);
            //绘制该点验证我们的想法
            g.DrawLine(Pens.DarkOliveGreen, o1.X + newPoint.X - 5, o1.Y-newPoint.Y, o1.X + newPoint.X+5, o1.Y- newPoint.Y);
            g.DrawLine(Pens.DarkOliveGreen, o1.X + newPoint.X , o1.Y - newPoint.Y-5, o1.X + newPoint.X, o1.Y - newPoint.Y+5);
            g.DrawString("  验证点", font,Brushes.Gray, newPoint.X+o1.X,o1.Y-newPoint.Y-6);

            g.DrawString("该点在新坐标系位置：[" + ((int)newPoint.X) + ":" + ((int)newPoint.Y) + "]", font, Brushes.Black, point1.X + 5, point1.Y +15);
            
            //绘制需要换算坐标系的点
        }

        private PointF Transition(float _x, float _y, double _angle,int fx)
        {
            double newX =0;
            double newY=0;
            if (fx == 1)
            {
                 newX = _x * Math.Cos(_angle) - _y * Math.Sin(_angle) ;
                 newY = _x * Math.Sin(_angle) + _y * Math.Cos(_angle) ;
            }
            else
            {
                 newX = _x * Math.Cos(_angle) + _y * Math.Sin(_angle) ;
                 newY = _x * Math.Sin(_angle) - _y * Math.Cos(_angle) ;
            }
            Console.WriteLine("x轴："+newX);
            Console.WriteLine("y轴："+newY);
          
            return new PointF((float)newX, (float)newY);
        }


        private void 直角坐标系转换_MouseMove(object sender, MouseEventArgs e)
        {
            txt_x.Text = e.X.ToString();
            txt_y.Text = e.Y.ToString();
        }
    }
}
